*------------------------------------------------------------------------------------------------------------%	
* Figure 2: Wage distributions at three large retailers after a company $15 minimum wage
*------------------------------------------------------------------------------------------------------------%	
	* Extract information on $15 wage policies at three large retailers from events dataset
	use "$data/cb/events.dta", clear
	keep if mw==15 
	rename mdate mwdate
	replace mwdate=mwdate
	keep cmp mwdate mw
	tempfile mw15
	save `mw15'
	
	* Merge in policy date from events dataset
	use "$data/cb/clean_policy_firm_national.dta", clear
	merge m:1 cmp using `mw15', nogen
	
	* Generate event time variable
	gen etime=.
	replace etime=mdate-mwdate-1 

	* Create weighted average of share less than, share at, and share above $15 min wage across all 3 companies
	collapse (sum) wage_lt15 wage_15 wage_gt16 wage_gt18 wage_lt30 wage_30, by(etime)
	replace etime=etime
	gen share_wage_lt15=wage_lt15/(wage_lt30+wage_30)
	gen share_wage_15=wage_15/(wage_lt30+wage_30)
	gen share_wage_gt15=(wage_gt16-wage_gt18)/(wage_lt30+wage_30)

	la var share_wage_lt15 "Wage <$15"
	la var share_wage_15 "Wage = $15"
	la var share_wage_gt15 "$15 < Wage < $18"

	* Plot wage distribution changes around company policies
	tsset etime
	twoway line share_wage_lt15 etime if inrange(etime,-12,11), lpattern(-.) lcolor("$dnwblue")   ///
	|| line share_wage_15 etime if inrange(etime,-12,11), lcolor("$dnwcrimson") ///
	|| line share_wage_gt15 etime if inrange(etime,-12,11), lpattern(dash) lcolor("$dnwgold") ///
	 legend( rows(1) ring(0) pos(1) size(small)) xtitle("Months since $15 MW") ytitle("Share of workers") graphregion(margin(l+0 r+5)) ysize(3.5) xsize(5) xline(-0.5, lcolor(gs8) lpattern(dash))
	 graph export "$figures_tables/fig2_wage_distribution_15vmw_slides.pdf", replace

	* Plot wage distribution changes around company policies
	tsset etime
	twoway line share_wage_lt15 etime if inrange(etime,-12,11), lwidth(thick) lpattern(-.) lcolor("$dnwblue")   ///
	|| line share_wage_15 etime if inrange(etime,-12,11), lwidth(thick)  lcolor("$dnwcrimson") ///
	|| line share_wage_gt15 etime if inrange(etime,-12,11), lwidth(thick)  lpattern(dash) lcolor("$dnwgold") ///
	 legend( rows(1) ring(0) pos(1) size(small)) xtitle("Months since $15 MW") ytitle("Share of workers") graphregion(margin(l+0 r+5))  xline(-0.5, lcolor(gs8) lpattern(dash))
	 graph export "$figures_tables/fig2_wage_distribution_15vmw.pdf", replace

	 
* Other MW level graphs
	* Extract information on MW wage policies at three large retailers from events dataset
	use "$data/cb/events.dta", clear
	keep if inrange(mw,9,15)
	levelsof mw,local(minw)
	foreach w in `minw'{	
		use "$data/cb/events.dta", clear
		keep if mw==`w' 
		rename mdate mwdate
		keep cmp mwdate mw
		tempfile mw`w'
		save `mw`w''	
		* Merge in policy date from events dataset
		use "$data/cb/clean_policy_firm_national.dta", clear
		merge m:1 cmp using `mw`w'', nogen
		
		* Generate event time variable
		gen etime=.
		replace etime=mdate-mwdate 

		* Create weighted average of share less than, share at, and share above $15 min wage across all 3 companies
		local wp=`w'+1
		local w_ub=`w'+3
		collapse (sum) wage_lt`w' wage_`w' wage_gt`w_ub' wage_gt`wp' wage_lt30 wage_30, by(etime)
		replace etime=etime+1
		gen share_wage_lt`w'=wage_lt`w'/(wage_lt30+wage_30)
		gen share_wage_`w'=wage_`w'/(wage_lt30+wage_30)
		gen share_wage_gt`w'=(wage_gt`wp'-wage_gt`w_ub')/(wage_lt30+wage_30)
		
		la var share_wage_lt`w' "Wage < $`w'"
		la var share_wage_`w' "Wage = $`w'"
		la var share_wage_gt`w' "$`w' < Wage < $`w_ub'"

		* Plot wage distribution changes around company policies
		tsset etime
		twoway line share_wage_lt`w' etime if inrange(etime,-12,11), lpattern(-.) lcolor("$dnwblue")   ///
		|| line share_wage_`w' etime if inrange(etime,-12,11),  lcolor("$dnwcrimson") ///
		|| line share_wage_gt`w' etime if inrange(etime,-12,11),lpattern(dash) lcolor("$dnwgold") ///
		 legend( rows(1) pos(1) size(small) region(color(none))) xtitle("Months since $`w' MW") ///
		 ytitle("Share of workers") graphregion(margin(l+0 r+5)) ysize(3.5) xsize(5) name(mw`w', replace)
		 *graph export "$figures_tables/appendix/share_mw`w'.pdf", replace
}
	#d ;

	graph combine mw9 mw10 mw11 mw12 mw13 mw14, rows(2) altshrink
	
	graphregion(color(white) margin(zero))
	
	
	l1title("Percent of workers", size(vsmall) ) 
	b1title("Months since MW", size(vsmall) ) 

	;
	#d cr

	graph export "$figures_tables/appendix/figb1_combined_share_around_mw.pdf", as(pdf) replace	
	
*------------------------------------------------------------------------------------------------------------%	
* Tables B1-B6: Checks on imputation in policy firm data
*------------------------------------------------------------------------------------------------------------%	
	
	use "$data/cb/stacked_policy_firm_imputation_check.dta", clear
	
* Build tables - full sample ---------------------------------------------------

* Share imputed
	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb1_outlier_sum_stats_insamp.txt", write replace

	file write outlier " \begin{tabular}{ccc} \toprule \toprule Variables & Share imputed & Share imputed (weighted) \\ \midrule " _n
	sum outlier_rate
	local unw: di %3.2f `r(mean)'
	sum outlier_rate [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Wage bins and employment & `unw' & `w' \\" _n
	sum outlier_ytd
	local unw: di %3.2f `r(mean)'
	sum outlier_ytd [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Running monthly earnings & `unw' & `w' \\" _n
	sum outlier_curr
	local unw: di %3.2f `r(mean)'
	sum outlier_curr [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Current monthly earnings* & `unw' & `w' \\" _n
	sum outlier_retain
	local unw: di %3.2f `r(mean)'
	sum outlier_retain [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Retention  & `unw' & `w' \\" _n
	file write outlier "\bottomrule \bottomrule \end{tabular} "

	file close outlier

	* NOTE that there are outliers in both reporting and values (outlier_curr)

* Share imputed by etime
	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb3_outlier_sum_stats_etime.txt", write replace
	file write outlier " \begin{tabular}{c|cccc} \toprule \toprule & \multicolumn{4}{c}{Share imputed} \\ Time from  & Employment & Running monthly & Current monthly & Retention  \\ event & and wage bins & earnings & earnings* &  \\ \midrule " _n
	foreach i of numlist -12/11 {
		qui sum outlier_rate if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "`i' & `mean'"
		qui sum outlier_ytd if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean'"
		qui sum outlier_curr if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean'"
		qui sum outlier_retain if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean' \\ " _n
	}
	file write outlier "\bottomrule \bottomrule \end{tabular} "

	file close outlier

* Coverage of currentmonthearn
	gen share_curr = tot_freq_currentmonthearn/tot_emp
	gen share_ytd = tot_freq_gross_pay_ytd_u/tot_emp

	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb5_tot_freq_curr_etime.txt", write replace
	file write outlier " \begin{tabular}{c|cc} \toprule \toprule & \multicolumn{2}{c}{Share w/ valid earnings} \\ Time from event & Running & Current \\ \midrule " _n
	foreach i of numlist -12/11 {
		sum share_ytd if etime == `i'
		local ytd: di %3.2f `r(mean)'
		sum share_curr if etime == `i' 
		local curr: di %3.2f `r(mean)' 
		file write outlier "`i' & `ytd' & `curr' \\" _n
	}
	file write outlier "\bottomrule \bottomrule \end{tabular} "
	file close outlier

* Share imputed by experiment by etime
	levelsof trt_exp, local(exps)
	foreach exp in `exps' {
		cap file close outlier
		file open outlier using "$figures_tables/appendix/outlier_sum_stats_etime_exp`exp'.txt", write replace
		file write outlier " \caption{Experiment `exp'} \begin{tabular}{c|cccc} \toprule \toprule & \multicolumn{4}{c}{Imputed} \\ Time from  & Employment & Running monthly & Current monthly & Retention  \\ event & and wage bins & earnings & earnings* &  \\ \midrule " _n
		
		preserve
		keep if trt_exp == `exp'
		
		foreach i of numlist -12/-1 {
			qui sum outlier_rate if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "`i' & `mean'"
			qui sum outlier_ytd if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean'"
			qui sum outlier_curr if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean'"
			qui sum outlier_retain if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean' \\ " _n
		}
		file write outlier "\midrule" _n
		foreach i of numlist 0/11 {
			qui sum outlier_rate if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "`i' & `mean'"
			qui sum outlier_ytd if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean'"
			qui sum outlier_curr if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean'"
			qui sum outlier_retain if etime == `i'
			local mean: di %3.2f `r(mean)'
			if "`mean'" == "0.00" {
				local mean "0"
			} 
			file write outlier "& `mean' \\ " _n
		}
		file write outlier "\bottomrule \bottomrule \end{tabular} "

		file close outlier
		
		restore
	}


* Build table - balanced (short) sample ----------------------------------------
	
* Make data restrictions
	keep if balanced_short == 1 & inrange(etime, -6, 5)
		
* Share imputed
	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb2_outlier_sum_stats_insamp_bal.txt", write replace

	file write outlier " \begin{tabular}{ccc} \toprule \toprule Variables & Share imputed & Share imputed (weighted) \\ \midrule " _n
	sum outlier_rate
	local unw: di %3.2f `r(mean)'
	sum outlier_rate [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Wage bins and employment & `unw' & `w' \\" _n
	sum outlier_ytd
	local unw: di %3.2f `r(mean)'
	sum outlier_ytd [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Running monthly earnings & `unw' & `w' \\" _n
	sum outlier_curr
	local unw: di %3.2f `r(mean)'
	sum outlier_curr [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Current monthly earnings* & `unw' & `w' \\" _n
	sum outlier_retain
	local unw: di %3.2f `r(mean)'
	sum outlier_retain [aw = tot_emp]
	local w: di %3.2f `r(mean)'
	file write outlier "Retention  & `unw' & `w' \\" _n
	file write outlier "\bottomrule \bottomrule \end{tabular} "

	file close outlier

	* NOTE that there are outliers in both reporting and values (outlier_curr)

* Share imputed by etime
	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb4_outlier_sum_stats_etime_bal.txt", write replace
	file write outlier " \begin{tabular}{c|cccc} \toprule \toprule & \multicolumn{4}{c}{Share imputed} \\ Time from  & Employment & Running monthly & Current monthly & Retention  \\ event & and wage bins & earnings & earnings* &  \\ \midrule " _n
	foreach i of numlist -6/5 {
		qui sum outlier_rate if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "`i' & `mean'"
		qui sum outlier_ytd if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean'"
		qui sum outlier_curr if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean'"
		qui sum outlier_retain if etime == `i'
		local mean: di %3.2f `r(mean)'
		if "`mean'" == "0.00" {
			local mean "0"
		} 
		file write outlier "& `mean' \\ " _n
	}
	file write outlier "\bottomrule \bottomrule \end{tabular} "

	file close outlier

* Coverage of currentmonthearn
	cap file close outlier
	file open outlier using "$figures_tables/appendix/tableb6_tot_freq_curr_etime_bal.txt", write replace
	file write outlier " \begin{tabular}{c|cc} \toprule \toprule & \multicolumn{2}{c}{Share w/ valid earnings} \\ Time from event & Running & Current \\ \midrule " _n
	foreach i of numlist -6/5 {
		sum share_ytd if etime == `i'
		local ytd: di %3.2f `r(mean)'
		sum share_curr if etime == `i' 
		local curr: di %3.2f `r(mean)' 
		file write outlier "`i' & `ytd' & `curr' \\" _n
	}
	file write outlier "\bottomrule \bottomrule \end{tabular} "
	file close outlier
	
*------------------------------------------------------------------------------------------------------------%	
* Tables B7: Experiments by MW level
*------------------------------------------------------------------------------------------------------------%	
	use "$data/cb/clean_policy_firm_cz", clear
	collapse (sum) tot_emp, by(cmp_company_code mdate)

	* Load
	merge 1:1 mdate cmp_company_code using "$data/cb/events.dta", keep(3) nogen

	* Drop exp 4
	drop if eventid == 4
	* Drop overlapping events
	drop if min(months_since, months_until) < 6

	* Collapse by MW
	gen count = 1
	collapse (rawsum) count (mean) share_affected [pw = tot_emp], by(mw)

	* Write tex table
	cap file close myfile
	file open myfile using "$figures_tables/appendix/tableb7_exp_by_mw.txt", write replace
	file write myfile "\begin{threeparttable} \begin{tabular}{ccc} \toprule \toprule" _n "Minimum wage & Number & Avg. share \\ (\\$) & policies & affected \\" _n "\midrule" _n
	sort mw
	foreach i of numlist 1/9 {
		local mw = mw[`i']
		local count = count[`i']
		local share : di %4.0g share_affected[`i']
		file write myfile "`mw' & `count' & `share' \\" _n
	}
	file write myfile "\bottomrule \bottomrule \end{tabular} \end{threeparttable}"
	file close myfile
	
	* Figure B2
	use "$data/cb/stacked_policy_wage_bin_dataset.dta", clear
	drop if trt_exp == 4
	
	*** Generate vars
	gen mw_dif = wage_bin - mw
	gen post = etime >= 0
	
	keep if inrange(etime, -6, 5)
	
	preserve
		collapse (mean) n_emp, by(post mw_dif)
		reshape wide n_emp, i(mw_dif) j(post)
		gen d_n_emp = (n_emp1 - n_emp0)/1000
		graph tw (bar d_n_emp mw_dif, color("$dnwblue")), ///
			xline(-0.5) yline(0) ///
			ytitle("Average change in monthly employment" "(thousands)", size(medium)) ///
			xtitle("Relative wage bin") ///
			ylab(-150(50)200) 
		graph export "$figures_tables/appendix/figb2a_bunching_all.pdf", replace
	restore
	
	preserve
		keep if share_affected>.3
		collapse (mean) n_emp, by(post mw_dif)
		reshape wide n_emp, i(mw_dif) j(post)
		gen d_n_emp = (n_emp1 - n_emp0)/1000
		graph tw (bar d_n_emp mw_dif, color("$dnwblue")), ///
			xline(-0.5) yline(0) ///
			ytitle("Average change in monthly employment" "(thousands)", size(medium)) ///
			xtitle("Relative wage bin") ///
			ylab(-150(50)200)
		graph export "$figures_tables/appendix/figb2b_bunching_major.pdf", replace
	restore
	
	preserve
		keep if mw==15
		collapse (mean) n_emp, by(post mw_dif)
		reshape wide n_emp, i(mw_dif) j(post)
		gen d_n_emp = (n_emp1 - n_emp0)/1000
		graph tw (bar d_n_emp mw_dif, color("$dnwblue")), ///
			xline(-0.5) yline(0) ///
			ytitle("Average change in monthly employment" "(thousands)", size(medium)) ///
			xtitle("Relative wage bin") ///
			ylab(-150(50)200)
		graph export "$figures_tables/appendix/figb2c_bunching_15.pdf", replace
	restore
	
	// Title: Difference in employment by wage bin at policy companies
	// Notes: Difference in average monthly employment at policy company before and after voluntary minimum wage event in thousands (up to 6 months prior to and 6 months following event). X axis centered at minimum wage level. Panel (a) includes all events; Panel (b) includes major events defined as those affecting at least 30% of the workforce; and Panel (c) includes $15 VMW events. Data sources: Large credit bureau.
